package com.pinyoumei.app.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.alipay.sdk.app.H5PayCallback;
import com.alipay.sdk.app.PayTask;
import com.alipay.sdk.util.H5PayResultModel;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.pinyoumei.app.R;
import com.pinyoumei.app.utils.EventBusUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.utils.event.AlipayEvent;

public class OtherWebViewActivity extends BaseActivityCurrency{

    private WebView otherWebview;
    private String data = "";
    private TextView includeTitle;

    @Override
    protected int layoutId() {
        return R.layout.other_webview;
    }

    @Override
    protected void initView() {
        otherWebview =  findViewById(R.id.other_webview);
        includeTitle = findViewById(R.id.include_title);
        data = getIntent().getStringExtra("data");
        includeTitle.setText(getIntent().getStringExtra("includeTitle"));
        findViewById(R.id.include_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        initWebView();
    }


    private void initWebView(){
        WebSettings settings = otherWebview.getSettings();
        // 设置WebView支持JavaScript
        settings.setJavaScriptEnabled(true);
        //支持自动适配
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(true);  //支持放大缩小
        settings.setBuiltInZoomControls(true); //显示缩放按钮
        settings.setBlockNetworkImage(true);// 把图片加载放在最后来加载渲染
        settings.setAllowFileAccess(true); // 允许访问文件
        settings.setSaveFormData(true);
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);/// 支持通过JS打开新窗口
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //设置不让其跳转浏览器
        otherWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

        // 添加客户端支持
        otherWebview.setWebChromeClient(new WebChromeClient());
        // mWebView.loadUrl(TEXTURL);

        //不加这个图片显示不出来
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            otherWebview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        otherWebview.getSettings().setBlockNetworkImage(false);

        //允许cookie 不然有的网站无法登陆
//        CookieManager mCookieManager = CookieManager.getInstance();
//        mCookieManager.setAcceptCookie(true);
//        mCookieManager.setAcceptThirdPartyCookies(webView, true);
        // 设置具体WebViewClient
        otherWebview.setWebViewClient(new OtherWebViewClient());

//        otherWebview.loadDataWithBaseURL(null,data, "text/html" , "utf-8", null);
        otherWebview.loadUrl(data);
    }

    class OtherWebViewClient extends WebViewClient{

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("alipays://platformapi")){
                boolean visit = checkAliPayInstalled(OtherWebViewActivity.this);
                if(visit){
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                }else{
                    ToastUtils.showShort("请安装支付宝支付");
                    return true;
                }
            }else{
                view.loadUrl(url);
            }

            finish();
            return true;
        }

    }

    //判断是否安装支付宝app
    public static boolean checkAliPayInstalled(Context context) {

        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }
}
